home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue69 / OpenGL / GLPickForm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-04-16  |  5.7 KB  |  211 lines

  1. unit GLPickForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   BasicOpenGL, OpenGL12, ComCtrls, ExtCtrls, StdCtrls, MMSystem;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     TV1: TTreeView;
  12.     Panel2: TPanel;
  13.     ZoomIn: TButton;
  14.     ZoomOut: TButton;
  15.     ViewPos: TComboBox;
  16.     StaticText1: TStaticText;
  17.     procedure FormShow(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure ZoomInClick(Sender: TObject);
  20.     procedure ZoomOutClick(Sender: TObject);
  21.     procedure TV1MouseUp(Sender: TObject; Button: TMouseButton;
  22.       Shift: TShiftState; X, Y: Integer);
  23.     procedure ViewPosChange(Sender: TObject);
  24.   private
  25.     { Private declarations }
  26.     Procedure UpdateTree;
  27.   public
  28.     { Public declarations }
  29.     OpenGLControl : TBasicOpenGL;  //manual instance of the Control
  30.     RootObject    : TOpenGLObject;  //Object set
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.DFM}
  39.  
  40. Function GetGLObject(Node:TTreeNode):TOpenGLObject;
  41. Begin
  42. //a helper which will extract the TOpenGLObject which is attached to the TreeView Node
  43.   Result:=nil;
  44.   If (Node=nil) or
  45.      (node.data=nil) then exit;
  46.   Result:=TOpenGLObject(Node.data);
  47. end;
  48.  
  49. procedure TForm1.FormShow(Sender: TObject);
  50.   Var Translate : TGLPoint;
  51.       Scale  : TGLPoint;
  52.       Rotate : TGLPoint;
  53.       TempGL,TempGL1,TempGL2: TOpenGLObject;
  54. begin
  55. //create the control
  56.   ViewPos.ItemIndex:=0;
  57.  
  58.   OpenGLControl := TBasicOpenGL.Create(self);
  59.   OpenGLControl.Parent:=self;
  60.   With OpenGLControl do
  61.   Begin
  62.     Align:=alClient;      //fill the client
  63.     GLStartUp;            //START UP THE GL SESSION
  64.     OnUpdateTreeView:=UpdateTree;   //set the call back to update the treeview
  65.   end;
  66.  
  67.   RootObject    := TOpenGLObject.create;  //create a root object
  68.   RootObject.Text:='Robot';
  69.   OpenGLControl.RootObject:=RootObject;   // connect the OpenGL object to the control
  70.  
  71.   Scale.X:=1;Scale.Y:=1;Scale.Z:=1;
  72.   Rotate.X:=0;Rotate.Y:=0;Rotate.Z:=0;
  73.   Translate.X:=0;Translate.X:=0;Translate.Z:=0;
  74.  
  75.     TempGL:= RootObject.addChild;
  76.     TempGL.Text:='Body';
  77.     Translate.X:=0; Translate.Y:=4;Translate.Z:=0;
  78.     Scale.X:=3;Scale.Y:=7;Scale.Z:=3;
  79.   //this X translation will also be applied to all children
  80.     TempGL.Translation:=Translate;
  81.     TempGL.scale:=scale;
  82.     TempGL.Mode:=2;
  83. //Head
  84.     TempGL1:=TempGL.AddChild;
  85.     TempGL1.Text:='Head';
  86.     TempGL1.Mode:=1;
  87.     Translate.X:=0; Translate.Y:=5.2;Translate.Z:=0;
  88.     Scale.X:=2.5;Scale.Y:=3;Scale.Z:=2.5;
  89.     TempGL1.Translation:=Translate;
  90.     TempGL1.Scale:=Scale;
  91.  
  92. //Left arm
  93.     TempGL1:=TempGL.AddChild;
  94.     TempGL1.Text:='Left_Arm';
  95.     TempGL1.Mode:=3;
  96.     Translate.X:=2.2; Translate.Y:=0;Translate.Z:=0;
  97.     Scale.X:=0.8;Scale.Y:=0.8;Scale.Z:=6;
  98.     Rotate.X:=90;
  99.     TempGL1.Scale:=Scale;
  100.     TempGL1.Translation:=Translate;
  101.     TempGL1.Rotation:=Rotate;
  102.  //Right arm
  103.     TempGL1:=TempGL.AddChild;
  104.     TempGL1.Text:='Right_Arm';
  105.     TempGL1.Mode:=3;
  106.     Translate.X:=-2.2; Translate.Y:=0;Translate.Z:=0;
  107.     Scale.X:=0.8;Scale.Y:=0.8;Scale.Z:=6;
  108.     Rotate.X:=90;
  109.     TempGL1.Scale:=Scale;
  110.     TempGL1.Translation:=Translate;
  111.     TempGL1.Rotation:=Rotate;
  112. //Left leg
  113.     TempGL1:=TempGL.AddChild;
  114.     TempGL1.Text:='Left_Leg';
  115.     TempGL1.Mode:=3;
  116.     Translate.X:=1; Translate.Y:=-7.5;Translate.Z:=0;
  117.     Scale.X:=1;Scale.Y:=1;Scale.Z:=8;
  118.     Rotate.X:=90;
  119.     TempGL1.Scale:=Scale;
  120.     TempGL1.Translation:=Translate;
  121.     TempGL1.Rotation:=Rotate;
  122.   //left foot
  123.     TempGL2:=TempGL1.AddChild;
  124.     TempGL2.Text:='Left_Foot';
  125.     TempGL2.Mode:=2;
  126.     Translate.X:=0; Translate.Y:=1;Translate.Z:=4.5;
  127.     Scale.X:=1;Scale.Y:=3;Scale.Z:=1;
  128.     TempGL2.Scale:=Scale;
  129.     TempGL2.Translation:=Translate;
  130. //Right leg
  131.     TempGL1:=TempGL.AddChild;
  132.     TempGL1.Text:='Right_Leg';
  133.     TempGL1.Mode:=3;
  134.     Translate.X:=-1; Translate.Y:=-7.5;Translate.Z:=0;
  135.     Scale.X:=1;Scale.Y:=1;Scale.Z:=8;
  136.     Rotate.X:=90;
  137.     TempGL1.Scale:=Scale;
  138.     TempGL1.Translation:=Translate;
  139.     TempGL1.Rotation:=Rotate;
  140.   //Right foot
  141.     TempGL2:=TempGL1.AddChild;
  142.     TempGL2.Text:='Right_Foot';
  143.     TempGL2.Mode:=2;
  144.     Translate.X:=0; Translate.Y:=1;Translate.Z:=4.5;
  145.     Scale.X:=1;Scale.Y:=3;Scale.Z:=1;
  146.     TempGL2.Scale:=Scale;
  147.     TempGL2.Translation:=Translate;
  148.  
  149.   UpdateTree;  // update the tree view
  150. end;
  151.  
  152. Procedure TForm1.UpdateTree;
  153.  Var
  154.      selectID: Integer;
  155. begin
  156. //attempt to keep the treeview in sync with the scene graph
  157. // I hate this control!!
  158.   TV1.Items.BeginUpdate;
  159.   If TV1.Selected<>Nil then
  160.      selectID:=TV1.Selected.AbsoluteIndex
  161.   else
  162.      selectID:=0;
  163.   TV1.Items.Clear;
  164.   RootObject.BuildTreeView(TV1,nil);
  165.   TV1.Selected:=TV1.Items[selectID];
  166.  
  167.   If TV1.Selected<>Nil then
  168.      TV1.Selected.Expand(True);
  169.   TV1.Items.EndUpdate;
  170. end;
  171.  
  172. procedure TForm1.FormDestroy(Sender: TObject);
  173. begin
  174.   RootObject.Free; //tidy up the GlOpbject
  175. end;
  176.  
  177. procedure TForm1.ZoomInClick(Sender: TObject);
  178. begin
  179.   OpenGLControl.ViewAngle:= OpenGLControl.ViewAngle*0.9;
  180.   //reduce the view angle to zoom in
  181. end;
  182.  
  183. procedure TForm1.ZoomOutClick(Sender: TObject);
  184. begin
  185.   OpenGLControl.ViewAngle:= OpenGLControl.ViewAngle/0.9;
  186.   //increase the view angle to zoom out
  187. end;
  188.  
  189. procedure TForm1.TV1MouseUp(Sender: TObject; Button: TMouseButton;
  190.   Shift: TShiftState; X, Y: Integer);
  191. Var GL:TOpenGLObject;
  192. begin
  193. //handle selection within the treeview
  194.   RootObject.Selected:=false;
  195.   If TV1.Selected=nil then exit;
  196.   GL:=GetGLObject(TV1.Selected);
  197.   If GL=nil then exit;
  198.   GL.Selected:=True;
  199.   UpdateTree;
  200.   If assigned(OpenGLControl) then
  201.      OpenGLControl.Repaint;
  202. end;
  203.  
  204. procedure TForm1.ViewPosChange(Sender: TObject);
  205. begin
  206.   OpenGLControl.ViewPosition:=ViewPos.ItemIndex;
  207.   OpenGLControl.repaint;
  208. end;
  209.  
  210. end.
  211.